package com.mystnihon.freeremote.ui;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.BounceInterpolator;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.mystnihon.freeremote.R;
import com.mystnihon.freeremote.db.table.SeedboxTable;

/**
 * this activity is displayed when the user select a torrent file or a torrent link
 * 
 * @author Utilisateur Niji
 */
public class AddTorrentFileActivity extends SherlockFragmentActivity implements LoaderCallbacks<Cursor>, OnClickListener {

    private static final int SEEDBOX_LOADER = 0;

    private Button mButtonAdd;

    private SeedboxCursorAdapter mSeedboxCursorAdapter;
    private Uri mUri;

    private Spinner mSpinner;
    private SeedboxTable mSelectedSeedbox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent_add_torrent);
        Intent intent = getIntent();
        mUri = intent.getData();

        // final View parent = findViewById(R.id.fr_scrollview);

        mSeedboxCursorAdapter = new SeedboxCursorAdapter(this, null, 0);

        mButtonAdd = (Button) findViewById(R.id.fr_add_torrent_in_seedbox);
        mButtonAdd.setOnClickListener(this);

        mSpinner = (Spinner) findViewById(R.id.fr_spinner_add);
        mSpinner.setAdapter(mSeedboxCursorAdapter);
        mSpinner.setOnItemSelectedListener(mSeedboxCursorAdapter);

    }

    @Override
    protected void onResume() {
        super.onResume();
        getSupportLoaderManager().initLoader(SEEDBOX_LOADER, null, this).forceLoad();
    }

    @Override
    public Loader<Cursor> onCreateLoader(int loaderId, Bundle bundle) {
        return new CursorLoader(this, SeedboxTable.CONTENT_URI, SeedboxTable.CONTENT_PROJECTION, null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        mSeedboxCursorAdapter.swapCursor(cursor);

    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mSeedboxCursorAdapter.swapCursor(null);

    }

    private class SeedboxCursorAdapter extends CursorAdapter implements OnItemSelectedListener {
        private LayoutInflater mInflater;

        public SeedboxCursorAdapter(Context context, Cursor c, int flags) {
            super(context, c, flags);
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public void bindView(View convertView, Context ctx, Cursor cursor) {
            final int position = cursor.getPosition();
            final SeedboxTable sdb = SeedboxTable.inflateObject(cursor);

            setData(convertView, sdb, position);
        }

        @Override
        public View newView(Context ctx, Cursor cursor, ViewGroup viewGroup) {
            final TextView convertView = (TextView) mInflater.inflate(R.layout.listitem_seedbox_add, null);

            final int position = cursor.getPosition();
            final SeedboxTable sdb = SeedboxTable.inflateObject(cursor);

            setData(convertView, sdb, position);
            return convertView;
        }

        @Override
        public View getDropDownView(int position, View view, ViewGroup parent) {
            return super.getDropDownView(position, view, parent);
        }

        public SeedboxTable getSelectedSeedbox(int position) {
            final Cursor c = getCursor();
            final int oldPosition = c.getPosition();
            if (c.moveToPosition(position)) {
                SeedboxTable sdb = SeedboxTable.inflateObject(c);
                c.moveToPosition(oldPosition);
                return sdb;
            }
            return null;

        }

        private void setData(View view, SeedboxTable seedbox, int position) {
            ((TextView) view).setText(seedbox.mName);
        }

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long arg3) {
            mSelectedSeedbox = getSelectedSeedbox(position);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            mSelectedSeedbox = null;
        }
    }

    @Override
    public void onClick(View view) {
        if (view == mButtonAdd) {

        }
    }

}
